package com.lw.edu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.commonutil.result.Result;
import com.lw.edu.entity.Teacher;
import com.lw.edu.entity.vo.TeacherQuery;
import com.lw.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lw
 * @since 2021-06-12
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    //1.查询讲师表中所有的数据
    @RequiresRoles( "nor_admin" )
    @ApiOperation( value = "查询所有讲师")
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.ok( teacherService.list( null ) );
    }
    //逻辑删除讲师
    @DeleteMapping("/{id}")
    public Result removeTeacher(@PathVariable("id") String id){
        boolean flag = teacherService.removeById( id );
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //分页查询讲师的方法(带条件)
    @GetMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageListTeacher(@PathVariable("current") Integer current,
                                  @PathVariable("limit") Integer limit,
                                  TeacherQuery teacherQuery){
        //创建page对象
        Page<Teacher> page = new Page<>( current, limit );
        IPage<Teacher> teacherIPage = teacherService.pageList( page, teacherQuery );
        return Result.ok( teacherIPage );
    }
    //添加讲师接口
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.save( teacher );
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getTeacher(@PathVariable("id") String id){
        Teacher teacher = teacherService.getById( id );
        return Result.ok( teacher );
    }
    //修改讲师
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean flag = teacherService.updateById( teacher );
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}

